package com.doit.day03

object _06_排序的相关练习 {
  def main(args: Array[String]): Unit = {
    val list: List[Order] = List(Order("u001", "o001", "g001", "c001", 19.9, 20),
      Order("u003", "o003", "g003", "c001", 29.9, 10),
      Order("u002", "o002", "g002", "c002", 49.9, 11),
      Order("u006", "o006", "g006", "c003", 39.9, 14),
      Order("u005", "o005", "g005", "c003", 79.9, 17),
      Order("u008", "o008", "g008", "c005", 59.9, 14),
      Order("u009", "o009", "g009", "c005", 89.9, 12),
      Order("u007", "o007", "g007", "c005", 99.9, 15),
      Order("u004", "o004", "g004", "c003", 49.9, 12),
      Order("u010", "o010", "g010", "c003", 29.9, 17))

/*    val grouped: Map[String, List[Order]] = list.groupBy(_.category_id)

    //接下来要做的事情是什么？？？
    val res: Map[String, Double] = grouped.map(tp => {
      val category_id: String = tp._1
      val all_amount: Double = tp._2.map(order => order.price * order.goods_num).sum
      (category_id, all_amount)
    })*/

    val grouped: Map[String, List[Order]] = list.groupBy(_.user_id)

    //接下来要做的事情是什么？？？
    val res: Map[String, Order] = grouped.map(tp => {
      val user_id: String = tp._1
      val orders: List[Order] = tp._2.sortBy(order => {
        order.price * order.goods_num
      }).reverse.take(1)
      (user_id, orders(0))
    })

    res.foreach(println)



//    ints.sortBy(word => -word).take(2)


/*    list.sortBy(tp=>{tp.goods_num * tp.price})
      .foreach(println)

    list.sortBy(tp=>{-tp.goods_num * tp.price})
      .foreach(println)*/


  }

}


case class Order(user_id:String,
                 order_id:String,
                 goods_id:String,
                 category_id:String,
                 price:Double,
                 goods_num:Int)
